﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class Dm_NhaPhanPhoiBLL
    {
        public Dm_NhaPhanPhoiBLL()
        {
        }

        public List<Dm_NhaPhanPhoi> RetrieveAllKichHoat()
        {
            List<Dm_NhaPhanPhoi> list = new List<Dm_NhaPhanPhoi>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.Dm_NhaPhanPhoi.Where(u => u.KichHoat).ToList();
            }
            return list;
        }

        public Dm_NhaPhanPhoi AddNewDmNhaPhanPhoi(Dm_NhaPhanPhoi objectDmNhaPhanPhoi)
        {
            Dm_NhaPhanPhoi dmNPPTemple = new Dm_NhaPhanPhoi();
            int error = -1;
            try
            {
                dmNPPTemple = objectDmNhaPhanPhoi;
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    entities.Dm_NhaPhanPhoi.Add(dmNPPTemple);
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmNPPTemple = null;
                    }
                }
            }
            catch
            {
            }
            return dmNPPTemple;
        }

        public bool UpdateDmNhaPhanPhoi(Dm_NhaPhanPhoi objectDmNhaPhanPhoi)
        {
            Dm_NhaPhanPhoi dmNPPTemple = new Dm_NhaPhanPhoi();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmNPPTemple = entities.Dm_NhaPhanPhoi.SingleOrDefault(c => c.MaNhaPhanPhoi == objectDmNhaPhanPhoi.MaNhaPhanPhoi);
                    dmNPPTemple.TenNhaPhanPhoi = objectDmNhaPhanPhoi.TenNhaPhanPhoi;
                    dmNPPTemple.DiaChi = objectDmNhaPhanPhoi.DiaChi;
                    dmNPPTemple.GhiChu = objectDmNhaPhanPhoi.GhiChu;
                    dmNPPTemple.KichHoat = objectDmNhaPhanPhoi.KichHoat;
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool DeleteDmNhaPhanPhoi(Dm_NhaPhanPhoi objectDmNhaPhanPhoi)
        {
            Dm_NhaPhanPhoi dmNPPTemple = new Dm_NhaPhanPhoi();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmNPPTemple = entities.Dm_NhaPhanPhoi.SingleOrDefault(c => c.MaNhaPhanPhoi == objectDmNhaPhanPhoi.MaNhaPhanPhoi);
                    entities.Dm_NhaPhanPhoi.Remove(dmNPPTemple);
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public List<Dm_NhaPhanPhoi> ListDmNhaPhanPhoi()
        {
            List<Dm_NhaPhanPhoi> dmNPPTemple = new List<Dm_NhaPhanPhoi>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi select c).ToList();
                }
            }
            catch
            {
            }
            return dmNPPTemple;
        }

        public List<Dm_NhaPhanPhoi> FindListDmNhaPhanPhoi(string maNPP, string tenNPP)
        {
            List<Dm_NhaPhanPhoi> dmNPPTemple = new List<Dm_NhaPhanPhoi>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (!string.IsNullOrEmpty(maNPP))
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Contains(maNPP)
                                       select c).ToList();
                    }
                    else if (!string.IsNullOrEmpty(tenNPP))
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Contains(tenNPP)
                                       select c).ToList();
                    }
                    else
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Contains(maNPP)
                                             && c.MaNhaPhanPhoi.Contains(tenNPP)
                                       select c).ToList();
                    }
                }
            }
            catch
            {
            }
            return dmNPPTemple;
        }

        public Dm_NhaPhanPhoi FindObjectDmNhaPhanPhoi(string maNPP, string tenNPP)
        {
            Dm_NhaPhanPhoi dmNPPTemple = new Dm_NhaPhanPhoi();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if (!string.IsNullOrEmpty(maNPP))
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Equals(maNPP)
                                       select c).SingleOrDefault();
                    }
                    else if (!string.IsNullOrEmpty(tenNPP))
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Equals(tenNPP)
                                       select c).SingleOrDefault();
                    }
                    else
                    {
                        dmNPPTemple = (from c in entities.Dm_NhaPhanPhoi
                                       where c.MaNhaPhanPhoi.Equals(maNPP)
                                             && c.MaNhaPhanPhoi.Equals(tenNPP)
                                       select c).SingleOrDefault();
                    }
                }
            }
            catch
            {
            }
            return dmNPPTemple;
        }

        public bool FindTrungMaDmNhaPhanPhoi(string maNPP)
        {
            Dm_NhaPhanPhoi dmNPPTemple = new Dm_NhaPhanPhoi();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmNPPTemple = entities.Dm_NhaPhanPhoi.SingleOrDefault(c => c.MaNhaPhanPhoi == maNPP);
                    if (dmNPPTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }
    }
}
